﻿namespace ApiTemplate.Common
{
    /// <summary>
    /// 操作office相关帮助类
    /// </summary>
    public partial class OfficelHelper
    {
        /// <summary>
        /// 导出excel到OSS
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="datas"></param>
        /// <param name="excelName"></param>
        /// <returns>返回OSS地址</returns>
        public static string ExportExcelToOss(object datas, string excelName)
        {
            using var stream = new MemoryStream();
            stream.SaveAs(datas);
            stream.Seek(0, SeekOrigin.Begin);
            return AliOSSHelper.FileUploadForStream(stream, excelName);
        }

        /// <summary>
        /// 从模板生成并上传至OSS
        /// </summary>
        /// <param name="datas">数据</param>
        /// <param name="excelName">要生成的文件名；如：学生信息.xlsx</param>
        /// <param name="apitemplateName">模板物理路径；如：C:\学生信息.xlsx</param>
        /// <returns>返回OSS地址</returns>
        public static string ExportExcelFromApiTemplateToOSS(object datas, string excelName, string apitemplateName)
        {
            using var stream = new MemoryStream();
            stream.SaveAsByTemplate(apitemplateName, datas);
            stream.Seek(0, SeekOrigin.Begin);
            return AliOSSHelper.FileUploadForStream(stream, excelName);
        }
    }
}
